package com.ccl.dao;

import com.ccl.entity.Article;
import com.ccl.entity.Statistic;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;


import java.util.List;

@Mapper
@Repository
public interface ArticleMapper {
    //查询所有文章
    @Select("select a.*,b.hits,b.comments_num from t_article a,t_statistic b"+
            " where a.id=b.article_id order by a.id desc")
    public List<Article> findAll();
    //根据文章id查询
    @Select("select * from t_article where id=#{id}")
    public Article findById(Integer id);
   /* //获取排行榜数据
    @Select("select * from t_statistic order by hits desc limit 10")
    public List<Article> getHeadData();*/
    //统计文章总数
    @Select("select count(*) from t_article ")
    public Integer getArticlesNum();
    //查询最新文章
    @Select("select a.*,b.hits,b.comments_num from t_article a,t_statistic b"+" " +
            "where a.id=b.article_id order by a.id desc limit #{num}")
    public List<Article> getRecentArticles(Integer num);
    //添加文章
    @Insert("insert into t_article(title,content,created,modified,categories,tags,allow_comment,thumbnail) " +
            "values(#{title},#{content},#{created},#{modified},#{categories},#{tags},#{allowComment},#{thumbnail})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    public void addArticle(Article article);
    //获取排行榜数据
    @Select("select a.*,b.hits,b.comments_num \n" +
            "from t_article a,t_statistic b \n" +
            "where a.id=b.article_id\n" +
            "order by hits desc\n" +
            "limit 10")
    public List<Article> getHeatData();

    //修改文章:ArticleMapper.xml形式实现
    public void updateArticle(Article article);

    //删除文章
    @Delete("delete from t_article where id=#{id}")
    public void deleteArticle(Integer id);
}
